#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QPalette>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    socket = new QTcpSocket(this);
    QPixmap pixmap = QPixmap(":/img/bg1.jpg");
    QPixmap pix = pixmap.scaled(this->size());
    QPalette pal;
    pal.setBrush(QPalette::Background,QBrush(pix));
    this->setPalette(pal);

    socket->connectToHost(QHostAddress("127.0.0.1"),8888);
    //连接成功时
    connect(socket,&QTcpSocket::connected,this,&Widget::connect_server);
    connect(socket,&QTcpSocket::disconnected,[=](){
        QMessageBox::information(this,"提示","已断开连接");
        this->close();
    });
    testpage = new test(socket);
    reg = new Register;
    log = new Login;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::connect_server()
{
    qDebug() << "连接服务器成功";
    connect_state = true;

}

void Widget::on_pushButton_test_clicked()
{
    if(!connect_state)
    {
        QMessageBox::critical(this,"连接","连接服务器失败");
        this->close();
    }
    else
    {
        if(!log->get_logstate())
        {
            QMessageBox::warning(this,"警告","先登录才能填写问卷!");
            return;
        }
        if(testpage->get_surState())
        {
            QMessageBox::warning(this,"警告","您已经填写过问卷了");
            return;
        }
        QString head = "question";
        socket->write(head.toLatin1());
        testpage->show();
    }
}



void Widget::on_pushButton_log_clicked()
{
    if(!connect_state)
    {
        QMessageBox::critical(this,"连接","连接服务器失败");
        this->close();
    }
    else
    {
        if(log->get_logstate())
        {
            QMessageBox::information(this,"提示","您已经登录过了");
            return;
        }
        log->show();
    }
}

void Widget::on_pushButton_reg_clicked()
{
    if(!connect_state)
    {
        QMessageBox::critical(this,"连接","连接服务器失败");
        this->close();
    }
    else
    {
        reg->show();
    }
}
